package com.order.controller;

import com.order.entity.Order;
import com.order.fegin.AccountFeignClient;
import com.order.fegin.AccountParam;
import com.order.fegin.StoreFeignClient;
import com.order.fegin.StoreParam;
import com.order.mapper.OrderMapper;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private AccountFeignClient accountFeignClient;
    @Autowired
    private StoreFeignClient storeFeignClient;

    @GlobalTransactional
    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/create")
    public void create() {

        Order order = new Order();
        order.setCode("XYJ");
        order.setName("洗衣机");
        order.setAccountNo("001");
        orderMapper.insert(order);


        AccountParam accountParam = new AccountParam();
        accountParam.setAccountNo("zxs");
        accountParam.setAmount(BigDecimal.ONE);
        accountFeignClient.reductionAmount(accountParam);

        StoreParam storeParam = new StoreParam();
        storeParam.setCode("xyj");
        storeParam.setCount(1);
        storeFeignClient.minCount(storeParam);

        //System.out.println(Long.valueOf("asd"));
    }
}
